package fr.mairie.parc.control.commande;

import java.util.ArrayList;
import java.util.List;

import org.apache.click.control.FieldSet;
import org.apache.click.control.Form;
import org.apache.click.control.Option;
import org.apache.click.control.Select;
import org.apache.click.control.TextArea;
import org.apache.click.control.TextField;
import org.apache.click.dataprovider.DataProvider;
import org.apache.click.extras.control.DateField;
import org.apache.click.extras.control.DoubleField;
import org.apache.click.extras.control.IntegerField;

import fr.mairie.parc.domain.Commande;

public class CommandeFields {

	private boolean withIdField;
	private IntegerField idCommande = new IntegerField ("idCommande", "ID");
	private FieldSet fieldset = new FieldSet("commandeFieldset", "Commande");
	private DateField dateCommande = new DateField("dateCommande", "Date",true);
	private TextField numDevis = new TextField("numDevis", "N° Devis",true);
	private TextField fournisseur = new TextField("fournisseur", "Fournisseur",true);
	private Select materiel = new Select ("materiel", "Materiel",true);
	private Select stock = new Select ("stock", "Stock",true);
	private TextField marque = new TextField("marque", "Marque",true);
	private TextField modele = new TextField("modele", "Modéle",true);
	private DoubleField prix = new DoubleField("prix", "Prix",true);
	private IntegerField quantite = new IntegerField("quantite", "Quantité",true);
	private TextArea commentaire = new TextArea("commentaire","Commentaire");

	@SuppressWarnings({ "rawtypes", "serial" })
	public CommandeFields (boolean withIdField) {
		this.withIdField = withIdField;

		if (withIdField) {
			fieldset.add(idCommande);
		}
		dateCommande.setFormatPattern("dd/MM/yyyy");
		fieldset.add(dateCommande);
		fieldset.add(numDevis);
		fieldset.add(fournisseur);

		materiel.setDefaultOption(new Option("materiel", "Choisir matériel"));
		materiel.setDataProvider(new DataProvider() {
				@SuppressWarnings({ "unchecked" })
				public List getData() {
	                List optionList = new ArrayList();
	                optionList.add(new Option("PC", "PC"));
	                optionList.add(new Option("Ecran", "Ecran"));
	                optionList.add(new Option("UniteCentral", "UniteCentral"));
	                optionList.add(new Option("Imprimante", "Imprimante"));
	                optionList.add(new Option("Scanner", "Scanner"));
	                optionList.add(new Option("Routeur", "Routeur"));
	                optionList.add(new Option("Serveur", "Serveur"));
	                optionList.add(new Option("Switch", "Switch"));
	                optionList.add(new Option("Traceur", "Traceur"));
	                optionList.add(new Option("PCPortable", "PCPortable"));
	                optionList.add(new Option("VideoProjecteur", "VideoProjecteur"));
	                optionList.add(new Option("TabletteGraphique", "TabletteGraphique"));
	                return optionList;
	            }
	        });
		fieldset.add(materiel);
		stock.setDefaultOption(new Option("stock", "Choisir stock"));
		stock.setDataProvider(new DataProvider() {
            @SuppressWarnings("unchecked")
			public List getData() {
                List optionList = new ArrayList();
                optionList.add(new Option("stockVille", "Stock ville"));
                optionList.add(new Option("stockEcole", "Stock école"));
                return optionList;
            }
        });
		fieldset.add(stock);
		fieldset.add(marque);
		fieldset.add(modele);
		fieldset.add(prix);
		fieldset.add(quantite);
		fieldset.add(commentaire);
	}

	public void addAll(Form form) {
		form.add(fieldset);
	}

	public void setReadonly(boolean disabled) {
		fieldset.setReadonly(disabled);
	}

	public void getFieldsValues(Commande in) {
		if (withIdField) {
			in.setIdCommande(idCommande.getInteger());
		}
		in.setDateCommande(dateCommande.getDate());
		in.setNumDevis(numDevis.getValue());
		in.setFournisseur(fournisseur.getValue());
		in.setMateriel(materiel.getValue());
		in.setStock(stock.getValue());
		in.setMarque(marque.getValue());
		in.setModele(modele.getValue());
		in.setPrix(prix.getDouble());
		in.setQuantite(quantite.getInteger());
		in.setCommentaire(commentaire.getValue());
	}

	public void setFieldsValues(Commande src) {
		if (withIdField) {
			idCommande.setInteger(src.getIdCommande());
		}
		dateCommande.setDate(src.getDateCommande());
		numDevis.setValue(src.getNumDevis());
		fournisseur.setValue(src.getFournisseur());
		materiel.setValue(src.getMateriel());
		stock.setValue(src.getStock());
		marque.setValue(src.getMarque());
		modele.setValue(src.getModele());
		prix.setDouble(src.getPrix());
		quantite.setInteger(src.getQuantite());
		commentaire.setValue(src.getCommentaire());
	}

	public void setIdCommande(Commande src) {
		idCommande.setInteger(src.getIdCommande());
	}

	public int getIdCommande() {
		return idCommande.getInteger();
	}
}
